define(['jquery', 'bootstrap', 'backend', 'addtabs', 'table', 'echarts', 'echarts-theme', 'template'], function($, undefined, Backend, Datatable, Table, Echarts, undefined, Template) {

    var Controller = {
        index: function() {
            const today = new Date(); // 今天日期
            const sevenDaysAgo = new Date();
            sevenDaysAgo.setDate(today.getDate() - 7);
            new Vue({
                el: '#app',
                data: {
                    message: 'Hello Vue2!',
                    option: {
                        xAxis: {
                            type: 'category',
                            boundaryGap: false,
                            data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
                        },
                        yAxis: {
                            type: 'value'
                        },
                        series: [{
                            data: [820, 932, 901, 934, 1290, 1330, 1320],
                            type: 'line',
                            areaStyle: {}
                        }]
                    },

                    option2: {
                        xAxis: {
                            type: 'category',
                            boundaryGap: false,
                            data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
                        },
                        yAxis: {
                            type: 'value'
                        },
                        series: [{
                            data: [820, 932, 901, 934, 1290, 1330, 1320],
                            type: 'line',
                            areaStyle: {}
                        }]
                    },
                    lineChart1: {},
                    lineChart2: {},
                    pickerOptions: {
                        shortcuts: [{
                            text: '昨天',
                            onClick(picker) {
                                const end = new Date();
                                const start = new Date();
                                start.setTime(start.getTime() - 3600 * 1000 * 24 * 1);
                                picker.$emit('pick', [start, end]);
                            }
                        }, {
                            text: '今天',
                            onClick(picker) {
                                const end = new Date();
                                const start = new Date();
                                picker.$emit('pick', [start, end]);
                            }
                        }, {
                            text: '上个月',
                            onClick(picker) {
                                const now = new Date();

                                // 当前月的第一天
                                const thisMonthFirst = new Date(now.getFullYear(), now.getMonth(), 1);
                                // 上个月的第一天
                                const lastMonthFirst = new Date(now.getFullYear(), now.getMonth() - 1, 1);
                                // 上个月的最后一天 = 当前月第一天 - 1天
                                const lastMonthLast = new Date(thisMonthFirst.getTime() - 24 * 60 * 60 * 1000);

                                picker.$emit('pick', [lastMonthFirst, lastMonthLast]);
                            }
                        }, {
                            text: '最近一周',
                            onClick(picker) {
                                const end = new Date();
                                const start = new Date();
                                start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                                picker.$emit('pick', [start, end]);
                            }
                        }, {
                            text: '最近一个月',
                            onClick(picker) {
                                const end = new Date();
                                const start = new Date();
                                start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                                picker.$emit('pick', [start, end]);
                            }
                        }, {
                            text: '最近三个月',
                            onClick(picker) {
                                const end = new Date();
                                const start = new Date();
                                start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                                picker.$emit('pick', [start, end]);
                            }
                        }]
                    },
                    time1: [sevenDaysAgo, today], // 默认选中 [今天, 今天]
                    time2: [sevenDaysAgo, today] // 默认选中 [今天, 今天]
                },
                created() {

                    var that = this


                    setTimeout(function() {
                        that.getData1(0, 1);
                        that.getData2(0, 1);
                        // that.show_line('line-chart2')
                    }, 1000);



                },
                methods: {
                    formatDate(date) {
                        const y = date.getFullYear()
                        const m = String(date.getMonth() + 1).padStart(2, '0')
                        const d = String(date.getDate()).padStart(2, '0')
                        return `${y}-${m}-${d}`
                    },
                    getData1(e, num = 0) {

                        if (num == 1) {
                            var startStr = this.formatDate(this.time1[0])
                            var endStr = this.formatDate(this.time1[1])
                        } else {
                            var startStr = this.formatDate(e[0])
                            var endStr = this.formatDate(e[1])
                        }

                        Fast.api.ajax({
                            url: 'dashboard/getOrderDataNum',
                            type: 'GET',
                            data: {
                                start: startStr,
                                endStr: endStr,
                            }
                        }, function(ret, res) {

                            if (res.code == 1) {
                                var myChart = Echarts.init(document.getElementById('line-chart1'), 'walden');

                                // 指定图表的配置项和数据
                                var option = {
                                    tooltip: {
                                        trigger: 'axis',
                                        
                                    },
                                    xAxis: {
                                        type: 'category',
                                        data: res.data.xAxis
                                    },
                                    yAxis: {
                                        type: 'value'
                                    },
                                    series: [{
                                        data: res.data.seriesData,
                                        type: 'line',
                                        label: {
                                            show: true, // 开启标签
                                            position: 'top' // 标签显示在点上方
                                        },
                                        // 可选美化：开启圆点
                                        symbol: 'circle',
                                        symbolSize: 8,
                                        itemStyle: {
                                            color: '#01C24F' // 折线颜色
                                        }
                                    }]
                                };
                                // 使用刚指定的配置项和数据显示图表。
                                myChart.setOption(option);
                                window.addEventListener('resize', () => {
                                    myChart.resize(); // 用箭头函数包装
                                });
                            }




                            return false
                        }, function(ret, res) {})
                    },
                    getData2(e, num = 0) {

                        if (num == 1) {
                            var startStr2 = this.formatDate(this.time1[0])
                            var endStr2 = this.formatDate(this.time1[1])
                        } else {
                           
                            var startStr2 = this.formatDate(e[0])
                            var endStr2 = this.formatDate(e[1])
                        }

                        Fast.api.ajax({
                            url: 'dashboard/getOrderDataPrice',
                            type: 'GET',
                            data: {
                                start: startStr2,
                                endStr: endStr2,
                            }
                        }, function(ret, res) {

                            if (res.code == 1) {
                                var myChart2 = Echarts.init(document.getElementById('line-chart2'), 'walden');

                                // 指定图表的配置项和数据
                                var option = {
                                    tooltip: {
                                        trigger: 'axis',
                                        formatter: function(params) {
                                            // params 是一个数组，每个 series 的一个项
                                            let result = params.map(item => {
                                                return `${item.axisValue}：订单金额：${item.data}`
                                            }).join('<br>');
                                            return result;
                                        }
                                    },
                                    xAxis: {
                                        type: 'category',
                                        data: res.data.xAxis
                                    },
                                    yAxis: {
                                        type: 'value'
                                    },
                                    series: [{
                                        data: res.data.seriesData,
                                        type: 'line',
                                        label: {
                                            show: true, // 开启标签
                                            position: 'top' // 标签显示在点上方
                                        },
                                        // 可选美化：开启圆点
                                        symbol: 'circle',
                                        symbolSize: 8,
                                        itemStyle: {
                                            color: '#01C24F' // 折线颜色
                                        }
                                    }]
                                };
                                // 使用刚指定的配置项和数据显示图表。
                                myChart2.setOption(option);
                                window.addEventListener('resize', () => {
                                    myChart2.resize(); // 用箭头函数包装
                                });
                            }




                            return false
                        }, function(ret, res) {})
                    },

                    changeMessage() {

                    }
                }
            });




        }
    };

    return Controller;
});